“h5_rdwt18”

Information

The example shown in this file appears at: http://www.hdfgroup.org/HDF5/Tutor/rdwt.html (accessed 10 July 2011)

The example shown in this file is following the tutorial: http://www.hdfgroup.org/HDF5/Tutor/introductory.html

Good references for the shown HDF5 functions include:
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5Front.html
http://www.hdfgroup.org/HDF5/doc/RM/

Prepared 10 July 2011 by Scot Martin for Windows 7, 64-bit, Mathematica version 8.0.1.

“c” code we seek to replicate in this example

/* 
 *   Writing and reading an existing dataset.
 */

#include "hdf5.h"
#define FILE "dset.h5"

int main() {

   hid_t       file_id, dataset_id;  /* identifiers */
   herr_t      status;
   int         i, j, dset_data[4][6];

   /* Initialize the dataset. */
   for (i = 0; i < 4; i++)
      for (j = 0; j < 6; j++)
         dset_data[i][j] = i * 6 + j + 1;

   /* Open an existing file. */
   file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);

   /* Open an existing dataset. */
   dataset_id = H5Dopen(file_id, "/dset", H5P_DEFAULT);

   /* Write the dataset. */
   status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
                     dset_data);

   status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
                    dset_data);

   /* Close the dataset. */
   status = H5Dclose(dataset_id);

   /* Close the file. */
   status = H5Fclose(file_id);
}

Implementation in Mathematica

SetDirectory[NotebookDirectory[]]

Needs["HDF5`"]

dsetData=Table[6i+j+1,{i,4},{j,6}]

file=FileNameJoin[{Directory[],"dset.h5"}]

fileID=H5Fopen[file,H5FuACCuRDWR,H5PuDEFAULT]

dataSetID=H5Dopen[fileID, "/dset", H5PuDEFAULT]

H5Dwrite[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,Flatten[dsetData]]

dataObject=NETNew["System.Int32[]",Length@Flatten@dsetData(*bufferSize*)]

H5Dread[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,dataObject]

H5Dclose[dataSetID]

H5Fclose[fileID]

Implementation in Mathematica using the Extend Functionality

This example is the same as above but written with a syntax that is closer to normal Mathematica style and uses the “HDF5Extend`” for this purpose.

SetDirectory[NotebookDirectory[]];
Needs["HDF5Extend`"];
dsetData=2Table[6i+j+1,{i,4},{j,6}]
file=FileNameJoin[{Directory[],"dset.h5"}]

H5FExtendOpen[
{fileID=H5Fopen[file,H5FuACCuRDWR,H5PuDEFAULT]},

H5DExtendOpen[
{dataSetID=H5Dopen[fileID, "/dset", H5PuDEFAULT]},

CompoundExpression[

(* write out the data *)
H5DExtendWrite[dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,dsetData,"DataType"->Integer32],

(* read the data back in *)
H5DExtendRead[dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,Dimensions@dsetData,"DataType"->Integer32]
]

] (* close H5DExtendOpen *)
] (* close H5FExtendOpen *)

Documentation of the employed Extend functions

??H5FExtendOpen

??H5DExtendOpen

??H5DExtendWrite

??H5DExtendRead